﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AgileDashboard.Entities;

namespace AgileDashboard.Services
{

    public class WorkItemReportingService
    {



        public Dictionary<WorkItem, Dictionary<string, double>> CalculateEpicStoryPointSummary(List<WorkItem> workItems)
            {

                //All of the epics have been added to the dictionary
                Dictionary<WorkItem, Dictionary<string, double>> EpicStoryPointSummary = new Dictionary<WorkItem, Dictionary<string, double>>();

                foreach (WorkItem us in workItems)
                {
                    AddEpic(EpicStoryPointSummary, us);
                    if (us.LinkedWorkedItems.Count > 0)
                    {
                        CalculateEpicStoryPointSummary(us.LinkedWorkedItems, EpicStoryPointSummary);
                    }
                }

                foreach (WorkItem key in EpicStoryPointSummary.Keys)
                {
                    CalculateEpicStoryPointSummary(key, EpicStoryPointSummary[key]);
                }


                return EpicStoryPointSummary;
        }



        public void CalculateEpicStoryPointSummary(List<WorkItem> workItems, Dictionary<WorkItem, Dictionary<string, double>> StoryPointSummary)
        {

            foreach (WorkItem us in workItems)
            {
                AddEpic(StoryPointSummary, us);
            }
        }


        private void AddEpic(Dictionary<WorkItem, Dictionary<string, double>> StoryPointSummary, WorkItem workItem)
        {
            if (workItem is UserStory)
            {

                if (((UserStory)workItem).IsEPIC)
                {
                    if (!StoryPointSummary.ContainsKey(workItem))
                    {
                        StoryPointSummary.Add(workItem, new Dictionary<string, double>());
                    }



                }
            }
        }



        public void CalculateEpicStoryPointSummary(WorkItem workItem, Dictionary<string, double> storyPointSummary)
        {

            foreach (WorkItem item in workItem.LinkedWorkedItems)
            {
                if (item is UserStory)
                {

                    UserStory us = (UserStory)item;

                    if (us.StoryPoints.HasValue)
                    {
                        if (storyPointSummary.ContainsKey(us.State))
                            storyPointSummary[us.State] = storyPointSummary[us.State] + us.StoryPoints.Value;
                        else
                        {
                            storyPointSummary.Add(us.State, us.StoryPoints.Value);
                        }

                    }

                    if (us.LinkedWorkedItems.Count > 0)
                    {
                        CalculateEpicStoryPointSummary(us, storyPointSummary);


                    }
                }
            }

        }


    }
}

